package com.wind.taobao.api.jianghu.convert
{
	import com.wind.taobao.api.jianghu.model.Feed;
	import com.wind.taobao.core.util.TaobaoConstants;

	import mx.collections.ArrayCollection;


	/**
	 * Feed 转换器
	 * @author fxy
	 *
	 */
	public class FeedConvert
	{
		/**
		 * 转换Feed集合
		 */
		public static function convertFeeds(value:Object, type:String="json"):ArrayCollection
		{
			var feeds:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							feeds.addItem(convertFeed((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return feeds;
		}

		/**
		 * 转换Feed
		 */
		public static function convertFeed(value:Object, type:String="json"):Feed
		{
			var feed:Feed=new Feed();
			if (value == null)
				return feed;
			if (type == TaobaoConstants.XML)
			{
				feed=convertXMLToFeed(XML(value), feed);
			}
			else
			{
				feed=convertJSONToFeed(value, feed);
			}
			return feed;
		}

		/**
		 * JSON转换为Feed
		 */
		private static function convertJSONToFeed(json:Object, feed:Feed):Feed
		{
			if (json.hasOwnProperty(TaobaoConstants.FEED_ID))
			{
				feed.feed_id=json[TaobaoConstants.FEED_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.USER_ICON))
			{
				feed.user_icon=json[TaobaoConstants.USER_ICON];
			}
			if (json.hasOwnProperty(TaobaoConstants.USER_ICON_LINK))
			{
				feed.user_icon_link=json[TaobaoConstants.USER_ICON_LINK];
			}
			if (json.hasOwnProperty(TaobaoConstants.CREATE_USER_ID))
			{
				feed.create_user_id=json[TaobaoConstants.CREATE_USER_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.PARTICIPATOR))
			{
				feed.participator=json[TaobaoConstants.PARTICIPATOR];
			}
			if (json.hasOwnProperty(TaobaoConstants.APP_ID))
			{
				feed.app_id=json[TaobaoConstants.APP_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.TITLE))
			{
				feed.title=json[TaobaoConstants.TITLE];
			}
			if (json.hasOwnProperty(TaobaoConstants.BODY))
			{
				feed.body=json[TaobaoConstants.BODY];
			}
			if (json.hasOwnProperty(TaobaoConstants.TITLE_LINK))
			{
				feed.title_link=json[TaobaoConstants.TITLE_LINK];
			}
			if (json.hasOwnProperty(TaobaoConstants.MEDIAS))
			{
				feed.medias=json[TaobaoConstants.MEDIAS];
			}
			if (json.hasOwnProperty(TaobaoConstants.GMT_CREATE))
			{
				feed.gmt_create=json[TaobaoConstants.GMT_CREATE];
			}
			if (json.hasOwnProperty(TaobaoConstants.WW_FEED_TYPES))
			{
				feed.ww_feed_types=json[TaobaoConstants.WW_FEED_TYPES];
			}
			return feed;
		}

		/**
		 * XML转换为Feed
		 */
		private static function convertXMLToFeed(value:XML, feed:Feed):Feed
		{
			return feed;
		}
	}
}